package com.example.sentinel;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

//1. 启动 sentinel 控制台，访问地址 127.0.0.1：8080
//2. 在jvm 中指定启动参数（应用名，sentinel控制台地址）-Dproject.name=sentinel-demo -Dcsp.sentinel.dashboard.server=127.0.0.1:8080


@RestController
public class SentinelController {
	
	// 接口A - QPS限流为2
	@GetMapping("/api/a")
	@SentinelResource(value = "apiA", blockHandler = "handleApiABlock")
	public String apiA() {
		return "This is API A response";
	}

	// 接口A的限流处理方法
	public String handleApiABlock(BlockException ex) {
		return "API A is blocked by Sentinel. Too many requests!";
	}

	// 接口B - QPS限流为5
	@GetMapping("/api/b")
	@SentinelResource(value = "apiB", blockHandler = "handleApiBBlock")
	public String apiB() {
		return "This is API B response";
	}

	// 接口B的限流处理方法
	public String handleApiBBlock(BlockException ex) {
		return "API B is blocked by Sentinel. Too many requests!";
	}

}
